<?php
namespace app\admin\controller\contents;
use app\common\controller\Backend;
class Zidian extends Backend{

    protected $relationSearch = false;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('zidian');
    }

    /**
     * 查看
     */
    public function index()
    {
        $input=input();
        $ids=$input['ids'] ?? 0;
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->request('filter');


            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $newWhere = ['cid'=>$ids];
            $total = $this->model
                ->where($newWhere)
                ->where($where)

                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($newWhere)
                ->where($where)

                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                if($v['cid']==0){
                    $v['count']=Model('zidian')->where(array('cid'=>$v['id']))->count();
                }
               // $v['atime'] = date("Y-m-d H:i:s",$v['atime']);
              //  $v['order_state_desc']=order_state($v);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig("ids", $this->request->param("ids"));
        return $this->view->fetch();
    }
    public function add($ids = NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('cid','title','porder','fname'))
            ));
            if ($params) {
                Model('zidian')->insert($params);
                $this->success();
            }
            $this->error();
        }
        if($ids>0){
            $row =Model('zidian')->where(array('id'=>$ids))->find();
            $this->view->assign('row',$row);
        }
        $cid=(int) $this->request->param("cid");
        if($cid>0){
            $cinfo=Model('zidian')->where(array('id'=>$cid))->find();
            $this->view->assign('ctitle',$cinfo['title']);
        }else{
            $this->view->assign('ctitle','顶级分类');
            $cid=0;
        }
        $this->view->assign('cid',$cid);
        return $this->view->fetch();
    }
    public function edit($ids = NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('title','fname'))
            ));
            if($ids<5 || $ids==33){
                unset($params['title']);
                unset($params['fname']);
            }
            $params['porder']=$_POST['row']['porder'];
            if ($params) {
                Model('zidian')->where(array('id'=>$ids))->update($params);
                $this->success();
            }
            $this->error();
        }
        if($ids>0){
            $row =Model('zidian')->where(array('id'=>$ids))->find();
            $this->view->assign('row',$row);
            $this->view->assign('ctitle',$row['title']);
        }else{
            $this->view->assign('ctitle','顶级分类');
        }
        return $this->view->fetch();
    }
    public function xuanxiang($cid = 0){
        if($cid<1){
            $this->error('参数错误');
        }
        // $info=Model('zidian')->where(array('id'=>$cid))->find();

        $list = Model('zidian')->where(array('cid'=>$cid))->select();
        $total = count($list);
        $result = array("total" => $total, "rows" => $list);
        if ($this->request->isAjax()) {
            return json($result);
        }
        return $this->view->fetch();
    }

}
